﻿using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using O2S.Components.PDFRender4NET;
using System.Drawing;
using System.IO;
using System.Text;

namespace Hr.Orc.Client.Helper
{
    public class PdfHelper
    {
        /*        public static string PdfToImage(Stream pdfStream, string directory, string fileName)
                {
                    PDFFile pdfFile = PDFFile.Open(pdfStream);
                    Bitmap pageImage = pdfFile.GetPageImage(0, 56 * 5);
                    pdfFile.Dispose();
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }
                    string imagePath = Path.Combine(directory, $"pdfToImg_{fileName}.png");
                    pageImage.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png);
                    return imagePath;
                }*/
        #region pdf转图片
        public static string PdfAllToImage(PDFFile pdfFile, string directory, string fileName)
        {
            var fristPageBitmap = pdfFile.GetPageImage(0, 56 * 3);
            // 获取 PDF 文档的总页数
            int pageCount = pdfFile.PageCount;
            string imagePath = Path.Combine(directory, $"pdfToImg_{fileName}.png");
            // 创建一个 Bitmap，大小为所有页面的总和
            Size totalSize = new Size(
                 Convert.ToInt32(fristPageBitmap.Width),
                 Convert.ToInt32(fristPageBitmap.Height) * pageCount
            );
            using (Bitmap totalBitmap = new Bitmap(totalSize.Width, totalSize.Height))
            {
                using (Graphics graphics = Graphics.FromImage(totalBitmap))
                {
                    // 遍历所有页面
                    for (int i = 0; i < pageCount; i++)
                    {
                        // 渲染 PDF 页面到图片
                        using (Bitmap pageBitmap = pdfFile.GetPageImage(i, 56 * 3))
                        {
                            // 将渲染的页面图片绘制到 totalBitmap
                            graphics.DrawImage(pageBitmap, 0, i * Convert.ToInt32(fristPageBitmap.Height), pageBitmap.Width, pageBitmap.Height);
                        }
                    }
                }
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                totalBitmap.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png);
            }
            return imagePath;
        }
        public static string PdfAllToImage(Stream pdfStream, string directory, string fileName)
        {
            // 创建 PDF 渲染器
            using (PDFFile pdfFile = PDFFile.Open(pdfStream))
            {
                return PdfAllToImage(pdfFile, directory, fileName);
            }
        }
        public static string PdfAllToImage(string pdfpath, string directory, string fileName)
        {
            // 创建 PDF 渲染器
            using (PDFFile pdfFile = PDFFile.Open(pdfpath))
            {
                return PdfAllToImage(pdfFile, directory, fileName);
            }
        }
        #endregion

        #region Pdf转图片分页

        public static List<string> PdfToImage(Stream pdfStream, string directory, int pageSize)
        {
            // 创建 PDF 渲染器
            using (PDFFile pdfFile = PDFFile.Open(pdfStream))
            {
                return PdfToImage(pdfFile, directory, pageSize);
            }
        }
        public static List<string> PdfToImage(string pdfpath, string directory, int pageSize)
        {
            using (PDFFile pdfFile = PDFFile.Open(pdfpath))
            {
                return PdfToImage(pdfFile, directory, pageSize);
            }
        }
        public static List<string> PdfToImage(PDFFile pdfFile, string directory, int pageSize)
        {
            var fristPageBitmap = pdfFile.GetPageImage(0, 56 * 10);
            // 获取 PDF 文档的总页数
            int pdfPageNumber = pdfFile.PageCount;
            var totalPageCount = (pdfPageNumber / pageSize) + (pdfPageNumber % pageSize == 0 ? 0 : 1);
            List<string> imagePaths = new List<string>();
            for (int pageIndex = 0; pageIndex < totalPageCount; pageIndex++)
            {
                string imagePath = Path.Combine(directory, $"pdfToImg_{pageIndex}.png");
                // 创建一个 Bitmap，大小为所有页面的总和
                Size totalSize = new Size(
                     Convert.ToInt32(fristPageBitmap.Width),
                     Convert.ToInt32(fristPageBitmap.Height) * pageSize
                );
                using (Bitmap totalBitmap = new Bitmap(totalSize.Width, totalSize.Height))
                {
                    using (Graphics graphics = Graphics.FromImage(totalBitmap))
                    {
                        // 遍历所有页面
                        for (int i = 0; i < pageSize; i++)
                        {
                            var pdfPage = pageIndex * pageSize + i;
                            if (pdfPage >= pdfPageNumber) break;
                            // 渲染 PDF 页面到图片
                            using (Bitmap pageBitmap = pdfFile.GetPageImage(pdfPage, 56 * 10))
                            {
                                // 将渲染的页面图片绘制到 totalBitmap
                                graphics.DrawImage(pageBitmap, 0, i * Convert.ToInt32(fristPageBitmap.Height), pageBitmap.Width, pageBitmap.Height);
                            }
                        }
                    }
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }
                    totalBitmap.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png);
                }
                imagePaths.Add(imagePath);
            }
            return imagePaths;
        }
        #endregion
        #region pdf转文字
        public static string ConvertPdfToText(string pdfbase64)
        {
            byte[] bytes = Convert.FromBase64String(pdfbase64);
            using (var pdfStream = new MemoryStream(bytes)) { 
                return ConvertPdfToText(pdfStream);
            }
        }
        public static string ConvertPdfToText(Stream pdfStream)
        {
            StringBuilder sb = new StringBuilder();
            using (var reader = new PdfReader(pdfStream))
            {
                using (var pdfDoc = new PdfDocument(reader))
                {
                    var pageNum = pdfDoc.GetNumberOfPages();
                    for (int i = 1; i <= pageNum; i++)
                    {
                        var page = pdfDoc.GetPage(i);
                        sb.Append(PdfTextExtractor.GetTextFromPage(page));
                    }
                }
            }
            return sb.ToString();
        }

        public static string PdfReadText(string pdfPath)
        {
            StringBuilder sb = new StringBuilder();
            using (PdfReader reader = new PdfReader(pdfPath))
            {
                using (PdfDocument pdfDoc = new PdfDocument(reader))
                {
                    for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
                    {
                        PdfPage page = pdfDoc.GetPage(i);
                        sb.Append( PdfTextExtractor.GetTextFromPage(page));
                    }
                }
            }
            return sb.ToString();

        }
        #endregion
    }
}
